editable.js ➔ fail   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
1
$(document).ready(function() {
2
    function fail(element, title, message) {
3
        element.addClass('ajax_save_failed');
4
        $.midcom_services_uimessage_add({
5
            type: 'error',
6
            title: title,
7
            message: message
8
        });
9
        return false;
10
    }
11
12
    $('.ajax_editable')
13
        .on('focus', function() {
14
            $(this)
15
                .data('orig-value', $(this).val())
16
                .addClass('ajax_focused');
17
        })
18
        .on('blur', function() {
19
            $(this).removeClass('ajax_focused');
20
            var ajaxUrl = this.dataset.ajaxUrl,
21
                element = $(this);
22
            if (!ajaxUrl) {
23
                throw 'Handler URL for cannot be found';
24
            }
25
26
            if (element.val() !== element.data('orig-value')) {
27
                element.addClass('ajax_saving');
28
                $.post(ajaxUrl, {guid: this.dataset.guid, title: element.val()}, function(data) {
29
                    element.removeClass('ajax_saving');
30
                    if (data.status !== true) {
31
                        return fail(element, 'Saving failed', data.message);
32
                    }
33
                    element.addClass('ajax_saved');
34
                    setTimeout(function() {
35
                        element.removeClass('ajax_saved');
36
                        element.addClass('ajax_saved_fade');
37
                    }, 1000);
38
                    setTimeout(function() {
39
                        element.removeClass('ajax_saved_fade');
40
                    }, 2000);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
41
                })
42
                    .fail(function() {
43
                        fail(element, 'Request failed', 'Request failed');
44
                    });
45
            }
46
47
            //If empty return to the default value (if one is provided)
48
            if (   $(this).val() == ''
49
                && this.dataset.ajaxDefault) {
50
                $(this).val(this.dataset.ajaxDefault);
51
            }
52
        });
53
});
54